//
//  TestViewController.m
//  MVVM+RAC
//
//  Created by bopeng on 16/5/25.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "TestViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "RedView.h"
#import "NavViewController.h"
#import <MapKit/MapKit.h>

@interface TestViewController ()

@property (weak, nonatomic) IBOutlet RedView *redView;
@property (nonatomic, assign) NSInteger age;

@property (weak, nonatomic) IBOutlet UIButton *btn;

@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (weak, nonatomic) IBOutlet UILabel *upLabel;

@property (weak, nonatomic) IBOutlet UILabel *bottomLabel;

@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.upLabel.text = @"了解了家乐福离开了就爱了房间数量发家垃圾了房间爱立方就垃圾分类及发链接垃圾分类发酵拉斯卡飞机拉法基发垃圾费拉飞机离开撒酒疯奥拉夫骄傲了";
    self.bottomLabel.text = @"测试一下下面了离开房间时发垃圾费老师及安防发觉了解放了发粉丝福利骄傲放假了无法哈家乐福按连接发垃圾分类卡号反馈及垃圾分类及安防机辣椒粉拉拉飞机垃圾费啦";
    
    // 1.代理
    [self delegate];

    // 2.KVO
    [self obserVer];
    
    // 3.监听事件
    [self event];
    
    // 4.监听通知
    [self notifacation];
    
    // 5.监听文本框文字改变
    [self textChange];
    
    // 6.场景：一个界面有2个请求，需要等两个请求都完成才刷新UI
    [self updateUI];
    
    [self packet];
}

- (void)dealloc
{
    NSLog(@"%s",__FUNCTION__);
}

#pragma mark - 代理
- (void)delegate {
    
#pragma clang diagnostic ignored "-Wundeclared-selector"
    @weakify(self);
    [[_redView rac_signalForSelector:@selector(btnClick:)] subscribeNext:^(id x) {
        @strongify(self);
        NavViewController *navVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:NSStringFromClass([NavViewController class])];
        navVC.title = @"透明";
        [self.navigationController pushViewController:navVC animated:YES];
    }];
}

#pragma mark - KVO
- (void)obserVer {
//    [[self rac_valuesForKeyPath:@"age" observer:nil] subscribeNext:^(id x) {
//        NSLog(@"%@",x);
//    }];
    
    [RACObserve(self, age) subscribeNext:^(id x) {
        NSLog(@"%@",x);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.age ++;
    [self.textField resignFirstResponder];
}

#pragma mark - 监听事件
- (void)event {
    
    @weakify(self);
    [[self.btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        NSLog(@"监听按钮点击事件---%@",self.textField.text);
        
        // 1. 创建地理反编码对象
        CLGeocoder *geocoder = [CLGeocoder new];
        
        // 2.实现地理反编码
        [geocoder geocodeAddressString:self.textField.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            
            // 3.获取最后一个地标对象 ---> 创建MKPlacemark对象
            MKPlacemark *placeMark = [[MKPlacemark alloc] initWithPlacemark:placemarks.lastObject];
            // 4.根据MKPlacemark对象 ---> 创建目的地所在的MKMapItem对象
            MKMapItem *destinationItem = [[MKMapItem alloc] initWithPlacemark:placeMark];
            // 5.获取起点位置
            MKMapItem *startItem = [MKMapItem mapItemForCurrentLocation];
            
            NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving};
            [MKMapItem openMapsWithItems:@[startItem,destinationItem] launchOptions:launchOptions];
        }];
    }];
}

#pragma mark - 监听通知
- (void)notifacation {
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillShowNotification object:nil] subscribeNext:^(id x) {
        NSLog(@"弹出键盘");
    }];
}

#pragma mark - 监听文本框文字改变
- (void)textChange {
    [[self.textField rac_textSignal] subscribeNext:^(id x) {
        NSLog(@"text:%@",x);
    }];
}

#pragma mark - 多个请求完成之后再进行下一步操作
- (void)updateUI {
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSLog(@"数据请求1");
        [subscriber sendNext:@"发送请求1回来的数据"];
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"随便写写");
        }];
    }];
    
    RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSLog(@"数据请求2");
        [subscriber sendNext:@"发送请求2回来的数据"];
        return nil;
    }];
    
    // RAC:就可以判断两个信号有没有都发出内容
    // SignalsFromArray:监听哪些信号的发出
    // 当signals数组中的所有信号都发送sendNext就会触发方法调用者(self)的selector
    // 注意:selector方法的参数不能乱写,有几个信号就对应几个参数
    // 不需要主动订阅signalA,signalB,方法内部会自动订阅
    [self rac_liftSelector:@selector(doneNextWithA:andB:) withSignalsFromArray:@[signalA,signalB]];
}

- (void)doneNextWithA:(NSString *)a andB:(NSString *)b {
    NSLog(@"请求完成，更新UI--a:%@,b:%@",a,b);
}

#pragma mark - RACTuple
- (void)packet{
    // 把参数中的数据包装成元组
    RACTuple *tuple = RACTuplePack(@"Scott_Mr",@20);
    
    // 解包元组，会把元组的值，按顺序给参数里面的变量赋值
    // name = @"Scott_Mr" age = @20
    RACTupleUnpack(NSString *name,NSNumber *age) = tuple;
    NSLog(@"%@,%@",name,age);
}

- (void)unPacket{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
